//
//  ProfilePhotoCell.m
//  League
//
//  Created by JackChen on 2018/2/19.
//  Copyright © 2018年 ciyuanjuzhen. All rights reserved.
//

#import "ProfilePhotoCell.h"
#import "ImgCollectionViewCell.h"
#import "gkhPickerImage.h"
#import "UserImageModel.h"

@interface ProfilePhotoCell ()<UICollectionViewDelegate, UICollectionViewDataSource>

@end

@implementation ProfilePhotoCell

- (void)awakeFromNib {
    [super awakeFromNib];
    self.collection.delegate = self;
    self.collection.dataSource = self;
}
//collectionView delegate
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 6;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    static NSString *reuseIdentifier = @"ImgCollectionViewCell";
    UINib *nib = [UINib nibWithNibName:@"ImgCollectionViewCell" bundle:[NSBundle mainBundle]];
    [collectionView registerNib:nib forCellWithReuseIdentifier:reuseIdentifier];
    ImgCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
    cell.backgroundColor = [UIColor whiteColor];
    cell.typeLab.frame = CGRectMake(0, 0, 0, 0);
    cell.imgView.frame = CGRectMake(0, 0, cell.height*0.2, cell.height*0.2);
    cell.imgView.image = [UIImage imageNamed:@"add"];
    cell.imgView.layer.cornerRadius = 5;
    cell.imgView.layer.masksToBounds = YES;
    cell.imgView.center = CGPointMake(cell.width/2, cell.height/2);
    
    if (indexPath.row < self.imageArr.count && self.imageArr.count != 0) {
        UserImageModel *model = self.imageArr[indexPath.row];
        [cell.imgView sd_setImageWithURL:[NSURL URLWithString:model.picUrl] placeholderImage:[UIImage imageNamed:@"add"] completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
            //改变 + 大小
            if ([cell.imgView.image isEqual:[UIImage imageNamed:@"add"]]) {
                cell.imgView.frame = CGRectMake(0, 0, cell.height*0.2, cell.height*0.2);
                cell.imgView.center = CGPointMake(cell.width/2, cell.height/2);
            } else {
                cell.imgView.frame = CGRectMake(0, 0, cell.height*0.9, cell.height*0.9);
                cell.imgView.center = CGPointMake(cell.width/2, cell.height/2);
            }
        }];
    }
    return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    return CGSizeMake(collectionView.width/3-1, collectionView.width/3-1);
}

-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    return UIEdgeInsetsMake(0, 0, 0, 0);
}

// 两行之间的最小间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
    return 0;
}

// 两个cell之间的最小间距，是由API自动计算的，只有当间距小于该值时，cell会进行换行
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
    return 0;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    self.chosePhoto(indexPath.item);
}

@end
